package com.m.utils;


import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.m.common.ReturnCode;
import com.m.enums.WebReturnCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
public class ResponseBuilder {

    private static final Logger logger = LoggerFactory.getLogger(ResponseBuilder.class);

    public static ResponseEntity build(String content) {
        return ResponseEntity.ok(content);
    }

    public static ResponseEntity build(WebReturnCode rt, String msg, Object data) {
        if (Strings.isNullOrEmpty(msg)) {
            msg = WebReturnCode.returnCodeMsg(rt);
        }
        ReturnCode returnCode = new ReturnCode(rt, msg, data);
        if (returnCode.getCode() != null && returnCode.getCode() != 0) {
            logger.info(JSONObject.toJSONString(returnCode));
        }
        return ResponseEntity.ok(returnCode);
    }

    public static ResponseEntity build(ReturnCode returnCode) {
        return ResponseEntity.ok(returnCode);
    }

}
